//
//  BaseViewController.swift
//  WB_Swift3.0
//
//  Created by wangrui on 2017/3/8.
//  Copyright © 2017年 wangrui. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController
{
    var isUserLogon = false
    lazy var visitorView = VisitorView(frame: UIScreen.main.bounds)
    lazy var navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    lazy var navItem = UINavigationItem()
    var tableView:UITableView?
    var refreshControl:UIRefreshControl?
    // 记录是否正在上拉刷新
    var mIsPull = false
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        navigationController?.navigationBar.isHidden = true
        setupUI()
        loadData()
        tableView?.reloadData()
    }
    
    override var title: String?
    {
        didSet {
            navItem.title = title
        }
    }
    
    func loadData()
    {
        refreshControl?.endRefreshing()
    }
    
}


extension BaseViewController
{
    fileprivate func setupUI()
    {
        view.backgroundColor = UIColor.cz_random()
        setupNavBar()
        // 取消自动缩进，当navigationBar隐藏后，会向上偏移20个点
        automaticallyAdjustsScrollViewInsets = false
        
        (isUserLogon == true) ? setupTableView() : setupVisitorView()
    }
    
    fileprivate func setupNavBar()
    {
        view.addSubview(navBar)
        navBar.items = [navItem]
        // 导航条背景颜色
        navBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        // 导航条标题颜色
        navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        // 导航条左右按钮字体颜色
        navBar.tintColor = UIColor.orange
    }
    
    func setupTableView()
    {
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navBar)
        tableView?.contentInset = UIEdgeInsetsMake(navBar.bounds.height,
                                                   0,
                                                   tabBarController?.tabBar.bounds.height ?? 49,
                                                   0)
        tableView?.dataSource = self
        tableView?.delegate = self
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        tableView?.addSubview(refreshControl!)
    }
    
    func setupVisitorView()
    {
        view.insertSubview(visitorView, belowSubview: navBar)
        visitorView.registerButton.addTarget(self, action: #selector(onClickRegister), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(onClickLogin), for: .touchUpInside)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(onClickRegister))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(onClickLogin))
    }
}


// MARK: - 注册和登录按钮的点击事件
extension BaseViewController
{
    @objc fileprivate func onClickRegister()
    {
        print("注册按钮")
    }
    
    @objc fileprivate func onClickLogin()
    {
        print("登录按钮")
    }
}

extension BaseViewController: UITableViewDataSource, UITableViewDelegate
{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
    {
        let section = indexPath.section
        let row = indexPath.row
        
        let sectionCount = tableView.numberOfSections
        if section == sectionCount - 1
        {
            let rowCount = tableView.numberOfRows(inSection: section)
            if (row == rowCount - 1) {
                // 当前是最后一个cell
                if (mIsPull == false)
                {
                    // 此时进行上拉刷新操作
                    print("上拉刷新")
                    mIsPull = true
                    loadData()
                }
            }
        }
    }
    
}





